# Maintainer: Alexey Pavlov <alexpux@gmail.com>

pkgname=('libxml2' 'libxml2-devel' 'libxml2-python')
pkgver=2.9.9
pkgrel=4
pkgdesc="XML parsing library, version 2"
arch=(i686 x86_64)
license=('MIT')
makedepends=('python2' 'python3' 'icu-devel>=59.1' 'libreadline-devel' 'ncurses-devel' 'liblzma-devel' 'zlib-devel')
url="http://www.xmlsoft.org/"
source=(http://xmlsoft.org/sources/${pkgname}-${pkgver}.tar.gz
        https://www.w3.org/XML/Test/xmlts20130923.tar.gz
        libxml2-2.9.0-do-not-check-crc.patch
        libxml2-2.7.3-doc-install.patch
        libxml2-cygwin-reentrant.patch
        libxml2-2.9.1-msys2.patch)
sha256sums=('94fb70890143e3c6549f265cee93ec064c80a84c42ad0f23e85ee1fd6540a871'
            '9b61db9f5dbffa545f4b8d78422167083a8568c59bd1129f94138f936cf6fc1f'
            '5a53d640ebce5a96e35fbdf5f644c37642dfa3345a1dc4fc355728897c19b7e7'
            'bff6790eadec6d7b3affd2d3a7385ce7f80cec2cf4abbd2ed83e9cb297a614a9'
            'fbd5ddb92e3cd0ab0f1f058436ca73b677f12fa00f65d634be7ef791e3706e33'
            '5b87437da7fa6fc83bc96dda29e3566fe56f4402a1f3edbd096cc9dc13e0aca4')

PYTHON_SITELIB=/usr/lib/python2.7/site-packages
PYTHON_LIB=/usr/lib/python2.7

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

del_file_exists() {
  for _fname in "$@"
  do
    if [ -f $_fname ] || [ -d $_fname ]; then
      rm -rf $_fname
    fi
  done
}
# =========================================== #


prepare() {
  cd ${srcdir}
  del_file_exists ${pkgname}-${pkgver}/xmlconf
  mv xmlconf -t ${pkgname}-${pkgver}

  cd ${pkgname}-${pkgver}

  apply_patch_with_msg \
    libxml2-2.9.0-do-not-check-crc.patch \
    libxml2-2.7.3-doc-install.patch \
    libxml2-cygwin-reentrant.patch \
    libxml2-2.9.1-msys2.patch 

  sed -e 's|/usr/bin/python -u|/usr/bin/python2 -u|g' -e 's|/usr/bin/python$|/usr/bin/python2|g' -i python/tests/*.py

  autoreconf -fi
}

build() {
  cd ${pkgname}-${pkgver}
  ./configure \
    --prefix=/usr \
    --sysconfdir=/etc \
    --localstatedir=/var \
    --build=${CHOST} \
    --host=${CHOST} \
    --target=${CHOST} \
    --enable-ipv6 \
    --with-history \
    --with-icu \
    --with-python=/usr/bin/python2 \
    --with-html-dir=/usr/share/doc \
    --with-html-subdir=libxml2/html
  make
  make DESTDIR=${srcdir}/dest install

  mv ${srcdir}/dest${PYTHON_SITELIB}/${pkgname/lib/msys-}mod.dll ${srcdir}/dest${PYTHON_SITELIB}/${pkgname}mod.dll
  sed -i -e "s:${pkgname/lib/msys-}mod:${PN}mod:g" ${srcdir}/dest${PYTHON_SITELIB}/${pkgname}mod.la
}

check() {
  cd ${pkgname}-${pkgver}
  make check
}

package_libxml2() {
  depends=('coreutils' 'icu>=59.1' 'liblzma' 'libreadline' 'ncurses' 'zlib')
  groups=('libraries')
  install=libxml2.install

  mkdir -p ${pkgdir}/usr
  cp -rf ${srcdir}/dest/usr/bin ${pkgdir}/usr/
  rm -f ${pkgdir}/usr/bin/*-config
  mkdir -p ${pkgdir}/usr/share
  cp -rf ${srcdir}/dest/usr/share/man ${pkgdir}/usr/share/
  install -Dm644 ${srcdir}/${pkgname}-${pkgver}/COPYING "${pkgdir}/usr/share/licenses/${pkgname}/COPYING"
}

package_libxml2-devel() {
  pkgdesc="Libxml2 headers and libraries"
  groups=('development')
  options=('staticlibs')
  depends=("libxml2=${pkgver}" 'icu-devel>=59.1' 'libreadline-devel' 'ncurses-devel' 'liblzma-devel' 'zlib-devel')

  mkdir -p ${pkgdir}/usr/{bin,share}
  cp -f ${srcdir}/dest/usr/bin/*-config ${pkgdir}/usr/bin/
  cp -rf ${srcdir}/dest/usr/include ${pkgdir}/usr/
  cp -rf ${srcdir}/dest/usr/lib ${pkgdir}/usr/
  rm -rf ${pkgdir}${PYTHON_SITELIB}
  cp -rf ${srcdir}/dest/usr/share/aclocal ${pkgdir}/usr/share/
  cp -rf ${srcdir}/dest/usr/share/doc ${pkgdir}/usr/share/
  rm -rf ${pkgdir}/usr/share/doc/python-libxml2
  cp -rf ${srcdir}/dest/usr/share/gtk-doc ${pkgdir}/usr/share/
}

package_libxml2-python(){
  pkgdesc="Libxml2 python modules"
  depends=('libxml2')
  groups=('python-modules')
  options=('staticlibs')
  install=libxml2-python.install

  mkdir -p ${pkgdir}/${PYTHON_SITELIB}
  mkdir -p ${pkgdir}/usr/share/doc
  cp -rf ${srcdir}/dest${PYTHON_SITELIB}/* ${pkgdir}${PYTHON_SITELIB}/
  cp -rf ${srcdir}/dest/usr/share/doc/python-libxml2 ${pkgdir}/usr/share/doc/
}
